package me.lc.superDown.sprite;

import me.lc.superDown.core.GameSystem;
import me.lc.superDown.sprite.tools.SpeedDownTool;
import me.lc.superDown.sprite.tools.SpeedUpTool;

import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.Blink;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

/**
 * 奔跑的人
 * 
 * @author Administrator
 * 
 */
public class RunGuy extends Node {
	Sprite manFaceRight;
	Sprite manFaceLeft;

	// 道具显示
	Sprite speedUp;
	Sprite speedDown;

	Animation animationRight;
	RepeatForever animateRight;

	Animation animationLeft;
	RepeatForever animateLeft;

	static float manWidth = ResolutionIndependent.resolveDp(40);
	static float manHeight = ResolutionIndependent.resolveDp(53);

	boolean isRightRunning = false;
	boolean isRuning = false;

	public RunGuy() {
		manFaceRight = Sprite.make(GameSystem.tex_manFaceRight, frameAt(0));
		animationRight = new Animation(0);
		animationRight.addFrame(0.15f, frameAt(1), frameAt(2), frameAt(3),
				frameAt(4), frameAt(5), frameAt(6));
		animateRight = RepeatForever.make(Animate.make(animationRight));

		this.addChild(manFaceRight);

		manFaceLeft = Sprite.make(GameSystem.tex_manFaceLeft, frameAt(0));
		animationLeft = new Animation(0);
		animationLeft.addFrame(0.15f, frameAt(1), frameAt(2), frameAt(3),
				frameAt(4), frameAt(5), frameAt(6));
		animateLeft = RepeatForever.make(Animate.make(animationLeft));

		this.addChild(manFaceLeft);
		manFaceLeft.setVisible(false);

		speedUp = Sprite.make(GameSystem.tex_speed_up, SpeedUpTool.frameAt(0));
		speedUp.setPosition(0, GameSystem.tex_manFaceRight.getHeight() / 2
				+ GameSystem.tex_speed_up.getHeight() / 2
				+ ResolutionIndependent.resolveDp(3));
		speedDown = Sprite.make(GameSystem.tex_speed_down,
				SpeedDownTool.frameAt(0));
		speedDown.setPosition(0, GameSystem.tex_manFaceRight.getHeight() / 2
				+ GameSystem.tex_speed_down.getHeight() / 2
				+ ResolutionIndependent.resolveDp(3));
		this.addChild(speedDown);
		this.addChild(speedUp);
		speedDown.setVisible(false);
		speedUp.setVisible(false);
	}

	public static WYRect frameAt(int x) {
		return WYRect.make(x * manWidth, 0, manWidth, manHeight);
	}

	public synchronized void runRight() {
		if (!isRuning || !isRightRunning) {
			manFaceRight.runAction(animateRight);
			manFaceRight.setVisible(true);
			manFaceLeft.setVisible(false);
			manFaceLeft.stopAllActions(true);
			isRightRunning = true;
			isRuning = true;
		}
	}

	public synchronized void runLeft() {
		if (!isRuning || isRightRunning) {
			manFaceLeft.runAction(animateLeft);
			manFaceLeft.setVisible(true);
			manFaceRight.setVisible(false);
			manFaceRight.stopAllActions(true);
			isRightRunning = false;
			isRuning = true;
		}
	}

	public synchronized void faceLeft() {
		manFaceLeft.setVisible(true);
		manFaceRight.setVisible(false);
	}

	public synchronized void faceRight() {
		manFaceLeft.setVisible(false);
		manFaceRight.setVisible(true);
	}

	public synchronized void stop() {
		manFaceRight.stopAllActions(true);
		manFaceLeft.stopAllActions(true);
		isRuning = false;
	}

	public float getManWidth() {
		return manWidth;
	}

	public synchronized void setSpeedUpTool() {
		this.speedUp.stopAllActions(true);
		this.speedDown.stopAllActions(true);
		this.speedUp.setVisible(true);
		this.speedDown.setVisible(false);
	}

	public synchronized void setSpeedDownTool() {
		this.speedUp.stopAllActions(true);
		this.speedDown.stopAllActions(true);
		this.speedUp.setVisible(false);
		this.speedDown.setVisible(true);
	}

	public synchronized void closeSpeedTool() {
		this.speedUp.stopAllActions(true);
		this.speedDown.stopAllActions(true);
		this.speedUp.setVisible(false);
		this.speedDown.setVisible(false);
	}

	public synchronized void setSpeedUpBlink() {
		IntervalAction blink = (IntervalAction) Blink.make(2f, 6).autoRelease();
		this.speedUp.runAction(blink);
	}

	public synchronized void setSpeedDownBlink() {
		IntervalAction blink = (IntervalAction) Blink.make(2f, 6).autoRelease();
		this.speedDown.runAction(blink);
	}
}
